# -*- coding:utf-8 -*-
# -------------------------------------------------------------------------------
# Description:  
# Reference:
# Author: 安智军
# Date:   2021/12/24
# -------------------------------------------------------------------------------
import datetime
import os
import time

import pytest

from common.get_allure_report import Getallurereport
from common.load_json import seconds_to_time, load_json
from common.send_Dingding import sendDingDing
from common.utils import Utils
from common.utils import project_dir
from common.logger import logger
from config.config import basepath


def main():
    test_data_path = project_dir + '/testData'
    logger.info(f'--测试数据的路径为：{test_data_path}')
    logger.info(f'---读取测试yml文件---')
    yml_file_name_list = Utils.get_test_data(test_data_path)
    logger.info(f"读取的yml文件列表是\n{str(yml_file_name_list)}")
    # print('yaml名称列表：', yml_file_name_list) # yaml名称列表： ['test_login.yml', 'test_Purchase_receipt_order_list.yml']
    out_put_dir = project_dir + '/testCases'
    logger.info(f"测试用例目录是{out_put_dir}")
    logger.info(f"先删除之前生成的pytest脚本")
    Utils.remove_old_script(out_put_dir)
    logger.info(f"正在生成pytest测试脚本")
    for yml_file_name in yml_file_name_list:
        # print(yml_file_name)
        logger.info(f"在生成{yml_file_name}的测试脚本")
        Utils.generate_test_script(yml_file_name,out_put_dir)
    logger.info(f"pytest脚本生成完成")

def generate_api_allure_report():
    os.system("allure generate ./Outputs/allure-results/ -o ./allure-report/ --clean")

def get_allure_result():
    # 读取测试报告结果
    file_path = basepath + '/allure-report/export/prometheusData.txt'
    file_time_path = basepath + '/allure-report/widgets/summary.json'
    result_dict = Getallurereport.get_allurereport_txt(file_path)
    time_datas = load_json(file_time_path)
    start_time = time_datas['time']['start']
    stop_time = time_datas['time']['stop']
    current_date = datetime.datetime.now().strftime('%Y-%m-%d')
    t_str = time.strftime('%H:%M:%S', time.localtime(start_time / 1000))  # 14:28:50
    t_stp = time.strftime('%H:%M:%S', time.localtime(stop_time / 1000))  # 14:28:50
    retries_run = result_dict.get('launch_retries_run')  # 运行总数
    status_passed = result_dict.get('launch_status_passed')  # 通过数量
    status_failed = result_dict.get('launch_status_failed')  # 不通过的数量
    status_broken = result_dict.get('launch_status_broken')  # 异常数量
    launch_time_duration_raw = result_dict.get('launch_time_duration')  # 总耗时毫秒
    launch_time_duration = seconds_to_time(int(launch_time_duration_raw) / 1000)
    pass_persent = '%.2f%%' % ((int(status_passed) / int(retries_run)) * 100)
    msg = " \n『WMS接口自动化测试报告』\n" \
          "日      期:  %s\n" \
          "开始执行时间:  %s\n" \
          "执行结束时间:  %s\n" \
          "总执行用例:  %s 个\n" \
          "成功用例数:  %s 个\n" \
          "失败用例数:  %s 个\n" \
          "错误用例数:  %s 个\n" \
          "测试通过率:  %s\n" \
          "用例执行总耗时：%s \n" \
          "具体详情请及时登录jenkins或在邮箱查看邮件" % (
          current_date, t_str, t_stp, retries_run, status_passed, status_failed, status_broken, pass_persent,launch_time_duration)
    logger.info(f'{msg}')
    return msg


if __name__ == "__main__":
    main()
    # pytest.main(['-v', '%s/testCases/'%basepath, '--alluredir=%s/Outputs/allure-results/'%basepath, '--clean-alluredir'])
    pytest.main(['-v', '%s/'%basepath, '--alluredir=%s/Outputs/allure-results/'%basepath, '--clean-alluredir'])
    generate_api_allure_report()
    msg = get_allure_result()
    # sendDingDing(msg)  # 发送钉钉 接口通畅 正式使用时启用